home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 80ZYRL (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  3.8 KB  |  150 lines

  1. package java.io;
  2.  
  3. import sun.io.ByteToCharConverter;
  4. import sun.io.ConversionBufferFullException;
  5.  
  6. public class InputStreamReader extends Reader {
  7.    private ByteToCharConverter btc;
  8.    // $FF: renamed from: in java.io.InputStream
  9.    private InputStream field_0;
  10.    private static final int defaultByteBufferSize = 8192;
  11.    // $FF: renamed from: bb byte[]
  12.    private byte[] field_1;
  13.    private int nBytes;
  14.    private int nextByte;
  15.  
  16.    public InputStreamReader(InputStream in) {
  17.       this(in, ByteToCharConverter.getDefault());
  18.    }
  19.  
  20.    public InputStreamReader(InputStream in, String enc) throws UnsupportedEncodingException {
  21.       this(in, ByteToCharConverter.getConverter(enc));
  22.    }
  23.  
  24.    private InputStreamReader(InputStream in, ByteToCharConverter btc) {
  25.       super(in);
  26.       this.nBytes = 0;
  27.       this.nextByte = 0;
  28.       this.field_0 = in;
  29.       this.btc = btc;
  30.       this.field_1 = new byte[8192];
  31.    }
  32.  
  33.    public void close() throws IOException {
  34.       synchronized(super.lock) {
  35.          if (this.field_0 == null) {
  36.             return;
  37.          }
  38.  
  39.          this.field_0.close();
  40.          this.field_0 = null;
  41.          this.field_1 = null;
  42.          this.btc = null;
  43.       }
  44.  
  45.    }
  46.  
  47.    private int convertInto(char[] cbuf, int off, int end) throws IOException {
  48.       int nc = 0;
  49.       if (this.nextByte < this.nBytes) {
  50.          try {
  51.             nc = this.btc.convert(this.field_1, this.nextByte, this.nBytes, cbuf, off, end);
  52.             this.nextByte = this.nBytes;
  53.             if (this.btc.nextByteIndex() != this.nextByte) {
  54.                this.malfunction();
  55.             }
  56.          } catch (ConversionBufferFullException var5) {
  57.             this.nextByte = this.btc.nextByteIndex();
  58.             nc = this.btc.nextCharIndex() - off;
  59.          }
  60.       }
  61.  
  62.       return nc;
  63.    }
  64.  
  65.    private void ensureOpen() throws IOException {
  66.       if (this.field_0 == null) {
  67.          throw new IOException("Stream closed");
  68.       }
  69.    }
  70.  
  71.    private int fill(char[] cbuf, int off, int end) throws IOException {
  72.       int nc = 0;
  73.       if (this.nextByte < this.nBytes) {
  74.          nc = this.convertInto(cbuf, off, end);
  75.       }
  76.  
  77.       while(off + nc < end) {
  78.          if (this.nBytes != -1) {
  79.             if (nc > 0 && !this.inReady()) {
  80.                break;
  81.             }
  82.  
  83.             this.nBytes = this.field_0.read(this.field_1);
  84.          }
  85.  
  86.          if (this.nBytes == -1) {
  87.             nc += this.flushInto(cbuf, off + nc, end);
  88.             if (nc == 0) {
  89.                return -1;
  90.             }
  91.             break;
  92.          }
  93.  
  94.          this.nextByte = 0;
  95.          nc += this.convertInto(cbuf, off + nc, end);
  96.       }
  97.  
  98.       return nc;
  99.    }
  100.  
  101.    private int flushInto(char[] cbuf, int off, int end) throws IOException {
  102.       int nc = 0;
  103.  
  104.       try {
  105.          nc = this.btc.flush(cbuf, off, end);
  106.       } catch (ConversionBufferFullException var5) {
  107.          nc = this.btc.nextCharIndex() - off;
  108.       }
  109.  
  110.       return nc;
  111.    }
  112.  
  113.    public String getEncoding() {
  114.       synchronized(super.lock) {
  115.          return this.btc != null ? this.btc.getCharacterEncoding() : null;
  116.       }
  117.    }
  118.  
  119.    private boolean inReady() {
  120.       try {
  121.          return this.field_0.available() > 0;
  122.       } catch (IOException var1) {
  123.          return false;
  124.       }
  125.    }
  126.  
  127.    private void malfunction() {
  128.       throw new InternalError("Converter malfunction (" + this.btc.getCharacterEncoding() + ") -- please submit a bug report via " + System.getProperty("java.vendor.url.bug"));
  129.    }
  130.  
  131.    public int read() throws IOException {
  132.       char[] cb = new char[1];
  133.       return this.read(cb, 0, 1) == -1 ? -1 : cb[0];
  134.    }
  135.  
  136.    public int read(char[] cbuf, int off, int len) throws IOException {
  137.       synchronized(super.lock) {
  138.          this.ensureOpen();
  139.          return this.fill(cbuf, off, off + len);
  140.       }
  141.    }
  142.  
  143.    public boolean ready() throws IOException {
  144.       synchronized(super.lock) {
  145.          this.ensureOpen();
  146.          return this.nextByte < this.nBytes || this.inReady();
  147.       }
  148.    }
  149. }
  150.